<?php
include('./init.php');
include('./helper.php');


isset($submit) or $submit='';
if($submit){
	
	$rst='';
	
	$src_fd=explode(',',$src_fd_str);
	$dst_fd=explode(',',$dst_fd_str);
	
	$data=$db->mselect("select {$src_fd_str} from {$src_table}");
	$cell=array();
	foreach($data as $k=>$v){
		$v[$src_fd[0]]=intval($v[$src_fd[0]]);
		$v[$src_fd[1]]=trim($v[$src_fd[1]]);
		$v[$src_fd[1]]=trim($v[$src_fd[1]],$sep);
		$v[$src_fd[1]]=str_replace(array(' ','　','，'),array('','',''),$v[$src_fd[1]]);
		
		isset($cell[$v[$src_fd[0]]]) or $cell[$v[$src_fd[0]]]=array();
		$cell[$v[$src_fd[0]]][]=intval($v[$src_fd[1]]);
		
		
	}

	foreach($cell as $a=>$b){
		$b=array_filter($b);
		$map=implode($sep,$b);
		
		//$rst.="insert into {$dst_table}(`{$dst_fd[0]}`,`{$dst_fd[1]}`) values ('{$a}','{$map}');\n";	
		//$rst.="{$a}\t{$map}\n";
		$rst.="update {$dst_table} set `{$dst_fd[1]}`='{$map}' where `{$dst_fd[0]}`='{$a}';\n";
		
	}
	
	if(isset($to_file)){
		header("Content-type: text/plain");
		header("Content-Disposition: attachment; filename=\"data.sql\"");
		header("Content-Length: ".strlen($rst));
		echo $rst;
		exit;
	
	}
	
}




?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>
<body>

<form method="post">
<table width="800" border="1">
  <tr>
    <th scope="row">来源表名</th>
    <td><input type="text" name="src_table" id="" value="<?php echo isset($src_table)?$src_table:'relation_comp';?>"></td>
  </tr>
  <tr>
    <th scope="row">来源表字段顺序</th>
    <td><input type="text" name="src_fd_str" id="" value="<?php echo isset($src_fd_str)?$src_fd_str:'product_id,item';?>">逗号分隔</td>
  </tr>
  <tr>
    <th scope="row">目标表名</th>
    <td><input type="text" name="dst_table" id="" value="<?php echo isset($dst_table)?$dst_table:'demo';?>"></td>
  </tr>
  <tr>
    <th scope="row">目标表字段顺序</th>
    <td><input type="text" name="dst_fd_str" id="" value="<?php echo isset($dst_fd_str)?$dst_fd_str:'product_id,infoids';?>"></td>
  </tr>
  <tr>
    <th scope="row">数据分隔符</th>
    <td><input type="text" name="sep" id="" value="<?php echo isset($sep)?$sep:',';?>"></td>
  </tr>
  <tr>
    <th scope="row">保存为文件</th>
    <td><input type="checkbox" name="to_file" id="" value="1" ></td>
  </tr>
  <tr>
    <th scope="row">结果</th>
    <td><textarea name="" cols="100" rows="10"><?php echo isset($rst)?$rst:'';?></textarea></td>
  </tr>
  <tr>
    <th scope="row">&nbsp;</th>
    <td align="center"><input type="submit" value="提交" name="submit" /></td>
  </tr>
</table>
</form>
<br />
<br />
<br />
<br />
<br />
<br />
<br />

<script src="jquery.js"></script>
<script>
$(document).ready(function(){
	
});
</script>
</body>
</html>
